package com.dc.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 *
 * @项目名称：common
 * @类名称：JSONUtils
 * @类描述：JackSon工具类
 * @version
 */
@SuppressWarnings("all")
public class JSONUtils2 {

	private final static ObjectMapper objectMapper = new ObjectMapper();

	private JSONUtils2() {
        //序列化的时候序列对象的所有属性
//        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

        //反序列化的时候如果多了其他属性,不抛出异常
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        //如果是空对象的时候,不抛异常
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
//        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

	private static final JSONUtils2 JSONUTIL = new JSONUtils2();

	public static JSONUtils2 getInstance() {
		return JSONUTIL;
	}

	/**
	 * javaBean,list,array convert to json string
	 * <br>对象转换为json字符串
	 */
	public static String obj2json(Object obj) throws JsonProcessingException {
		return objectMapper.writeValueAsString(obj);
	}

	/**
	 * json string convert to javaBean
	 * <br>json字符串转换为实体类对象
	 */
	public static <T> T json2pojo(String jsonStr, Class<T> clazz)
			throws Exception {
		return objectMapper.readValue(jsonStr, clazz);
	}

	/**
	 * json string convert to map
	 * <br>json字符串转换为Map<String, Object>对象
	 */
	public static <T> Map<String, Object> json2map(String jsonStr)
			throws Exception {
		return objectMapper.readValue(jsonStr, Map.class);
	}

	public static <T> Map<String, Object> obj2Map(T obj) throws Exception {
		return objectMapper.readValue(objectMapper.writeValueAsString(obj), Map.class);
	}

	/**
	 * json string convert to map<map> , convert to map with javaBean
	 * <br>json字符串转换为Map<String, Object>对象,然后再转换为Map<String, T>对象
	 */
//	public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz)
//			throws Exception {
//		Map<String, Map<String, Object>> map = objectMapper.readValue(jsonStr,
//				new TypeReference<Map<String, T>>() {
//				});
//		Map<String, T> result = new HashMap<String, T>();
//		for (Entry<String, Map<String, Object>> entry : map.entrySet()) {
//			result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
//		}
//		return result;
//	}

	/**
	 * json array string convert to list<map<String,Obj>>,convert to list<T> with javaBean
	 * <br>json字符串转换为Map<String, Object>对象,然后再转换为List<T>对象
	 */
//	public static <T> List<T> json2map2list(String jsonArrayStr, Class<T> clazz)
//			throws Exception {
//		List<Map<String, Object>> list = objectMapper.readValue(jsonArrayStr,
//				new TypeReference<List<T>>() {
//				});
//		List<T> result = new ArrayList<T>();
//		for (Map<String, Object> map : list) {
//			result.add(map2pojo(map, clazz));
//		}
//		return result;
//	}

	/**
	 * json array string convert to list with javaBean
	 * <br>json字符串转换为List<T>对象
	 */
	public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz)
			throws Exception {
		List<T> result = objectMapper.readValue(jsonArrayStr, objectMapper
				.getTypeFactory().constructCollectionType(List.class, clazz));
		return result;
	}

	/**
	 * map convert to javaBean
	 * <br>Map<String, Object>对象转换为<T>实体类对象
	 */
	public static <T> T map2pojo(Map map, Class<T> clazz) {
		return objectMapper.convertValue(map, clazz);
	}
}